package ru.stork.vasilisa.core

import java.io.File
import ru.stork.vasilisa.exceptions.VasilisaCompilerException

/**
 * This manager looks about things in the source paths.
 */
class SourceManager(val sourcePaths: List[String]) {

  /**
   * @param pgk List("com", "company")
   * @param name Clazz
   */
  def getSourceInPackage(pgk: List[String], name: String): Option[String] = {
    // relative path to the source
    val filePath = pgk.foldLeft("")((t,x) => t+"/"+x)+"/"+name+".vsl"
    // function which check source existense in root
    def findSource (root:String) = new File(root + filePath).exists
    // source paths (roots) conflict.
    if (sourcePaths.count(findSource) > 1)
      // TODO extend report
      throw new VasilisaCompilerException("Source files conflict")
    else sourcePaths.find(findSource) match {
      case Some(x) => Some(x + filePath)
      case None => None
    }
  }

}